package android.tkb.util.word;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class PageUtil {
	/**
	 * 隨機選取條數
	 * @param list
	 * @param count 顯示條數
	 * @return
	 * @author dingyuanwei 
	 * @version 創建時間：Aug 13, 2009 8:50:09 AM
	 */
	public static List randdomSub(List list,int count){
	    List ranResult = new ArrayList();
	    Random ran = new Random();
	    Set set=new LinkedHashSet();
	    Object[]obj;
	    if(count>=list.size()){
	    	count=list.size();
	    	while(set.size() < count){
	    		set.add(ran.nextInt(list.size()));
	    	}
	    }else{
	    	while(set.size() < count){
	    		set.add(ran.nextInt(list.size()));
	    	}
	    }
	    obj=set.toArray();
	    for(int i=0;i<obj.length;i++){
	    	Integer j=(Integer)obj[i];
	    	ranResult.add(list.get(j));
	    }	    
	    return ranResult;
	}
	
	public static void main(String[] args) {
		List list  = new ArrayList();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		List b = randdomSub(list,4);
		for (int i = 0; i < b .size(); i++) {
			System.out.println(list.get(i));
		}
	}
}
